package controller;

import helper.Formatter;
import helper.UserSession;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import model.*;
import org.mybeans.dao.DAOException;

import databeans.*;

/*
 * @Xiao Liang
 * Employee Use Case: View Payment History
 */
public class EmployeeViewCustomerPaymentHistoryAction extends Action{

	private PaymentDAO paymentDAO; 

	public EmployeeViewCustomerPaymentHistoryAction(Model model) {
		paymentDAO = model.getPaymentDAO(); 
	}

	public String getName() { return "employeeViewPaymentHistory.do"; }

	public String perform(HttpServletRequest request) {
        // define target page
		String loginPageAjax   = "common/messages.jsp";
		String loginPage = "employee/employee-login.jsp";
        String successPage = "employee/payment-history.jsp"; 
        String originPage  = successPage;
        
        // Set up the errors list
        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        String type = (String)request.getParameter("type");
        System.out.println("================= >>> type = " + type);
        
        Employee employee = null;
        Customer customer = null;
        
        //check if employee has been logged in
        //request.getSession() will return the current session and if one does not exist, a new session will be cretaed.
        //request.getSession(true) will return the current session if one exists, if one doesn't exits a new one will be created.
        //So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false), it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.
        employee = UserSession.getLoggedInEmployee(request);
        if (employee == null) {
        	if(type!=null && type.equals("ajax"))
        	{
        		errors.add("Your session has timed out. Please Login again. <a href=\"employeeHome.do\">Login</a> ");
        		return loginPageAjax;
        	}else
        	{
        		errors.add("You need to log in");
                return loginPage ;
        	}
        }
        
        //check if customer has actually logged in
        customer = UserSession.getLoggedInCustomer(request);
        if (customer != null) {
            request = UserSession.removeCustomerFromSession(request);
            errors.add("This section is closed for customer");
            return loginPage;
        }

        
		try {

	       
			//get customer id
	        String txtCustomerID = request.getParameter("customerID");
	        System.out.println(txtCustomerID);
	        if((txtCustomerID==null)||(txtCustomerID.trim().equals("")))return originPage; 
	        
  			int customerID = Integer.parseInt(txtCustomerID); 
  			Payment[] payments = paymentDAO.getPayments(customerID); 
  			request.setAttribute("customerID", txtCustomerID);
  			
  			List<Integer> transIDs = new ArrayList<Integer>();
  			List<String> transDates = new ArrayList<String>();
  			List<String> transTypes = new ArrayList<String>();
  			List<Long> amounts = new ArrayList<Long>(); 
  			List<String> statuses = new ArrayList<String>(); 
  			SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy");
  			
  			for(int i=0; i<payments.length; i++){
  				transIDs.add(payments[i].getPaymentID());
  				
  				//display transactionDate
  				transDates.add(format.format(payments[i].getSubmitDate()));
  				
  				//display transactionType
  				transTypes.add(Formatter.transactionType(payments[i].getTransactionType()));
  				
  				//display amount
  				amounts.add(payments[i].getAmount());
  				
  				//display status: Pending or Not Pending
  				boolean success = payments[i].isExecuted(); 
  				if (success) statuses.add("Approved"); 
  				else statuses.add("Pending");
  			}
  			request.setAttribute("transIDs", transIDs);
  			request.setAttribute("transDates", transDates);
  			request.setAttribute("transTypes", transTypes);
  			request.setAttribute("amounts", amounts);
  			request.setAttribute("statuses", statuses); 

	        return successPage;
        } catch (DAOException e) {
        	errors.add(e.getMessage());
        	return successPage;
        }
    }
	
}
